1278B - A and B - CodeForces Solution


greedy math *1500

Please click on ads to support us..

Python Code:

import sys
'''
1,2,...,n 的数组中选取j个数求和得sumj
abs(sumj*2-sumn)
4*0+1 [1,1]
4*0+2 [3,3]
3 [6, 0]
4 [10, 8]
5 [15, 5]
6 [21, 17]
7 [28, 12]
8 [36, 30]
9 [45, 23]
10 [55, 47]

1 1
2 3

g(n)=1+...+n

f(0)=[g[0],g(-1)+2]

f(1)=[g(1),1=g(-2)+2] 1
f(2)=[g(2),g(1)+2] 1

f(3)=[g(3),0=g(0)+2] 0
f(4)=[g(4),g(3)+2] 0

f(5)=[g(5),g(2)+2] 1
f(6)=[g(6),g(5)+2] 1

f(7)=[g(7),g(4)+2]
f(8)=[g(8),g(7)+2]

f(2n+1)=[(2n-1)+2

0*4+1 0 1
0*4+2 1 3 
1*4+1,2 [2,10] 21
2*4+1,2 [11,27]  55

0*4+3 3 6
0*4+4 5 10

2
13
212
1313
21212
131313
'''


def g(n):
    if n == -2:
        return -1
    if n == -1:
        return -2
    return n*(n+1)/2


def run(a, b):
    c = abs(a-b)
    i = int(((8*c+1)**0.5-1)/2)
    while 1:
        ma = g(i)
        if i % 2 == 0:
            mi = g(i-1)+2
        else:
            mi = g(i-3)+2
        if mi <= c and c <= ma:
            if i % 4 == 1 or i % 4 == 2:
                if c % 2 == 1:
                    return i
            else:
                if c % 2 == 0:
                    return i
        i += 1


if sys.argv[-1] == 't':
    cases = [
        [0, 19, 6],
        [0, 12, 7],
        [0, 15, 5],
        [1, 3, 3],
        [11, 11, 0],
        [30, 20, 4]
    ]
    for case in cases:
        r = run(*case[:-1])
        if r != case[-1]:
            print(case, r)
    print(run(0, 10**9)-int(((8*(10**9)+1)**0.5-1)/2))
    for i in range(1, 10**5):
        re = run(0, 0+i)
                                        
else:
    for _ in range(int(input())):
        print(run(*[int(d) for d in input().split(" ")]))

	  	 						 										 	 	  	 	

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#include <string>
#include <cmath>
#include <algorithm>

int main() {
	#define int long long
	int t; cin >> t;
	while (t--) {
		int a, b; cin >> a >> b;
		int diff=abs(a-b);
		int sum=0, ans=1;
		while (sum<diff) {
			sum+=ans;
			ans++;
		}
		if (sum==diff) {
			cout << ans-1 << endl;
		}
		else {
			if (sum%2==diff%2) {
				cout << ans-1 << endl;
			}
			else if (ans%2==1) {
				cout << ans << endl;
			}
			else {
				cout << ans+1 << endl;
			}
		}
	}
}


Comments

Submit
0 Comments
More Questions

733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences